package com.wlstock.funi.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.dao.MyAccountStockDao;
import com.wlstock.funi.dialog.PopTradeDialog;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.request.QuickBuy;
import com.wlstock.funi.request.QuickSaile;
import com.wlstock.funi.utils.IntentUtils;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.utils.ToastUtils;

public class TradeSelector implements QuickBuy.QuickBuyListener, QuickSaile.QuickSellListener {
	
	private Context mContext;
	private String stkName;
	private String stkCode;
	private String stkPrice;
	
	private TradeSelector(){
	}
	
	public TradeSelector(Context mContext,String stkName,String stkCode){
		this.mContext = mContext;
		this.stkName = stkName;
		this.stkCode = stkCode;
	}
	
	public TradeSelector(Context mContext,String stkName,String stkCode,String stkPrice){
		this(mContext,stkName,stkCode);
		this.stkPrice = stkPrice;
	}
	
	
	public void show(final int tradeType){
		String tradeMethod = "";
		
		if (tradeType == OperateModel.TradeType_BUY) {
			tradeMethod = "买入";
		}else if (tradeType == OperateModel.TradeType_SELL) {
			tradeMethod = "卖出";
		}else {
			return;
		}
		AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("选择交易方式").setItems(new String[]{tradeMethod,"闪电"+tradeMethod}, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					normalTrade(stkName,stkCode,tradeType);
					break;
				case 1:
					thunderTrade(stkName,stkCode,tradeType);
					break;
				default:
					break;
				}
				
			}
			
		}).setNegativeButton("取消", null).create();
		dialog.show();
	}
	
	
	private void normalTrade(String stkName, String stkCode, int tradeType) {
		if (tradeType == OperateModel.TradeType_BUY) {
			buy(false,stkName,stkCode);
		}else if (tradeType == OperateModel.TradeType_SELL) {
			sell(false,stkName,stkCode);
		}else {
			return;
		}
		
	}
	
	private void thunderTrade(String stkName, String stkCode, int tradeType) {
		if (tradeType == OperateModel.TradeType_BUY) {
			buy(true,stkName,stkCode);
		}else if (tradeType == OperateModel.TradeType_SELL) {
			sell(true,stkName,stkCode);
		}else {
			return;
		}
	}

	private void sell(boolean isThunder, String stkName, String stkCode) {
		final MyAccountStocks account = MyAccountStockDao.getInstance(mContext).queryStock(stkCode);
		if (account != null && account.getStkQty() != 0) {
			QuickSaile saile = new QuickSaile(stkCode, stkName, String.valueOf(account.getStkQty()));
			if (isThunder) {
				saile.saileDialog(mContext);
			}else if (stkPrice == null) {
				saile.qsListener = this;
				saile.saile(isThunder);
			}else {
				popTradeDialog(stkPrice, OperateModel.TradeType_SELL);
			}
		} else {
			ToastUtils.showMiddleMsg(mContext, "你没有持有!");		
		}
		
	}

	private void buy(boolean isThunder, String stkName, String stkCode) {
		QuickBuy buy = new QuickBuy(stkCode, stkName, (int) SharePreferenceUtils.getFloatSP(FundApplication.getInstance(), SharePrefConstants.DEFAULT_MONEY, 5000));
		if (isThunder) {
			buy.reqBuyDialog(mContext);
		}else if (stkPrice==null) {
			buy.qbListener = this;
			buy.reqBuy(isThunder);
		}else{
			popTradeDialog(stkPrice,OperateModel.TradeType_BUY);	
		}
	}



	
	private void popTradeDialog(String price,int tradeType) {
		Intent intent = new Intent(mContext, PopTradeDialog.class);
		MyAccountStocks ownStock = new MyAccountStocks();
		ownStock.setStkCode(stkCode);
		ownStock.setStkQty(100);
		ownStock.setCurPrice(String.valueOf(price));
		//配置intent
		IntentUtils.configWithOwnstock(intent, ownStock,tradeType);
		mContext.startActivity(intent);
	}


	@Override
	public void getBuyInfo(boolean isSuccess, String stkCode, String stkName, float price, QuickBuy.BUYMODE mode, String pty, int totalMoney) {
		if (isSuccess){
			popTradeDialog(String.valueOf(price), OperateModel.TradeType_BUY);
		}
	}

	@Override
	public void getSellInfo(boolean isSuccess, String stkCode, String stkName, float price, String pty) {
		if (isSuccess){
			popTradeDialog(String.valueOf(price),OperateModel.TradeType_SELL);
		}
	}
}
